package face.pay.web.device.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import face.pay.entity.DeviceApplyInfo;
import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.device.service.DeviceApplyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

/**
 * @Classname DeviceApplyInfoController
 * @Description JJL
 * @Date 2019/11/13 17:23
 * 设备申请
 */
@Controller
@RequestMapping("/apply")
public class DeviceApplyInfoController {

    @Autowired
    private DeviceApplyInfoService deviceApplyInfoService;

    /**
     * 代理商设备申请页面
     * @return
     */
    @RequestMapping("/list")
    public String manager(){
        return "device/agencyDeviceApplyInfoList";
    }

    /**
     * oem设备申请审批页面
     * @return
     */
    @RequestMapping("/oemList")
    public String oemManager(){
        return  "device/oemDeviceApplyInfoList";
    }

    /**
     * 查找用户设备
     * 分为用户可操作的设备
     * 和当前申请已经分配的设备
     * @param loginUser
     * @param applyId
     * @return
     */
    @RequestMapping("/queryUsesOperDevices")
    @ResponseBody
    public ResultMessage query(@CurrentUser UserInfo loginUser,Integer applyId){
        return deviceApplyInfoService.queryUsesOperDevices(loginUser,applyId);
    }

    @RequestMapping("/queryData")
    @ResponseBody
    public PageData queryData(@CurrentUser UserInfo loginUser,PageParam pageParam,DeviceApplyInfo deviceApplyInfo){
        return deviceApplyInfoService.queryPage(loginUser,pageParam,deviceApplyInfo);
    }

    /**
     * 新增代理商申请设备记录
     * @param deviceApplyInfo
     */
    @RequestMapping("/addApplyInfo")
    @ResponseBody
    public ResultMessage addDeviceApplyInfo(@CurrentUser UserInfo loginUser, DeviceApplyInfo deviceApplyInfo){
        return deviceApplyInfoService.addDeviceApplyInfo(loginUser,deviceApplyInfo);
    }

    /**
     * 设备申请  申请驳回
     * @param deviceApplyInfo
     * @return
     */
    @RequestMapping("/rejectApplyInfo")
    @ResponseBody
    public ResultMessage rejectApplyInfo(@CurrentUser UserInfo loginUser,DeviceApplyInfo deviceApplyInfo){
        return deviceApplyInfoService.rejectApplyInfo(loginUser,deviceApplyInfo);
    }

    /**
     * 设备申请审批   确认划拨
     * @param loginUser
     * @param transferData 划拨设备数据
     * @param applyId 设备申请记录id
     * @param userId 设备申请人id
     * @return
     */
    @RequestMapping("/transferDeviceInfo")
    @ResponseBody
    public ResultMessage transferDeviceInfo(@CurrentUser UserInfo loginUser,@RequestParam("transferData") String transferData,
                                            Integer applyId,
                                            Integer userId){

        ArrayList<DeviceApplyInfo> transferDeviceInfo = JSON.parseObject(transferData, new TypeReference<ArrayList<DeviceApplyInfo>>() {});

        return deviceApplyInfoService.transferDeviceInfo(loginUser,transferDeviceInfo,applyId,userId);
    }

    /**
     * 设备申请审批   暂存数据
     * @param loginUser
     * @param transferData 划拨设备数据
     * @param applyId 设备申请记录id
     * @return
     */
    @RequestMapping("/depositDeviceInfo")
    @ResponseBody
    public ResultMessage depositDeviceInfo(@CurrentUser UserInfo loginUser,@RequestParam("transferData") String transferData, Integer applyId){

        ArrayList<DeviceApplyInfo> transferDeviceInfo = JSON.parseObject(transferData, new TypeReference<ArrayList<DeviceApplyInfo>>() {});

        return deviceApplyInfoService.depositDeviceInfo(loginUser,transferDeviceInfo,applyId);
    }

    /**
     * 划拨完成数据详情
     * @param loginUser
     * @param applyId
     * @return
     */
    @RequestMapping("/details")
    @ResponseBody
    public ResultMessage details(@CurrentUser UserInfo loginUser,Integer applyId){
        return deviceApplyInfoService.detail(loginUser,applyId);
    }
}
